#include <stdio.h>
#include <string.h>
#include "cyassl/ctaocrypt/random.h"
#include "cyassl/ctaocrypt/sha.h"
#include <pari/pari.h>

void calc_hnf(int M, int *sample)        /* void */
{
  printf("M = %d\n",M);

  int i=0,j=0,count=0;;

  GEN p1;         /* vec */
  p1 = cgetg(M+1, t_MAT);

  for(i = 0; i < M; ++i) {
  		gel(p1, i+1) = cgetg(M+1, t_COL);
  }

  for(i=0;i<M;++i)
  		{
  			for(j=0;j<M;++j)
  			{
  				gcoeff(p1, i+1, j+1) = stoi(sample[count]);
  				printf("Mat[%d][%d] = %d\n",i,j,sample[count]);
  				count++;
  			}
  		}
  pari_printf("%Ps\n", p1);
  pari_printf("%Ps\n", mathnf0(p1, 0));
  return;
}

int main(void)
{
        printf("Hello 1 2 3 \n");
        pari_init(1000000,2);
        int arr[9] = {9, -36, 30, -36, 192, -180, 30, -180, 180};
        int arrSize = sizeof(arr)/sizeof(int);
        printf("Array size = %d\n", arrSize);
        calc_hnf((int) sqrt(arrSize), arr);


        byte hexstring[] = "0123456789abcdef";
        int i=0;
        int j = sizeof(hexstring)/sizeof(hexstring[0]);
        int array[j-1];

    	printf("%d\n",j);
    	for (i = 0; i < j; ++i)
    	{
    		printf("%c = ", hexstring[i]);
    		if (isdigit(hexstring[i]))
    		{
    			array[i] = hexstring[i] - '0';
    			printf("%d, ", array[i]);
    		}
    		else if( hexstring[i] >= 'a' && hexstring[i] <= 'f' )
    		{
    			array[i] = ( hexstring[i] - 'a' ) + 10;
    			printf("%d, ", array[i]);
    		}
    		else
    		{
    			printf("ERROR .... input is not a valid number in hex");
    		}
		}
    	printf("\nHello\n");
    	printf("%d\n", sizeof(array)/sizeof(array[0]));
    	for (i = 0; i < sizeof(array)/sizeof(int); ++i)
    	{
    		printf("%d, ", array[i]);
		}

    	int arraySize = sizeof(array)/sizeof(int);
    	calc_hnf((int) sqrt(arraySize), array);
    	pari_close();
        return 0;
}
